/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "run.h"
#include "ps2.h"
#include "delay.h"
#include <math.h>

#define L1MOTRO_MAX 7199
#define L2MOTRO_MAX 7199
#define R1MOTRO_MAX 7199
#define R2MOTRO_MAX 7199

#define SPIN_RATIO 0.8f

#define SERVO1_MAX 130
#define SERVO2_MAX 150

#define SERVO1_MIN 0
#define SERVO2_MIN 15

#define SERVO1_STEP 10
#define SERVO2_STEP 10

#define FAN1MOTRO_MAX 6000
#define FAN2MOTRO_MAX 6000
uint8_t key;
int data3, data4;
int j = 0, j1 = 0, k = 0, z = 0, g = 0, a1 = 0, a2 = 0, k1 = 0, temp = 0, temp1 = 0;
int16_t servo1_ang = SERVO1_MIN,servo2_ang=SERVO2_MAX;
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
void Control_1(int angle)
{
  int temp;
  temp = (100 / 9) * angle + 500;
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, (int)temp);
}
void Control_2(int angle)
{
  int temp;
  temp = (100 / 9) * angle + 500;
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, (int)temp);
}
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
void bopan_1()
{
  temp++;
  if (temp == 1)
  {
    j++;
    Control_2(j * 30);
    delay_us(100000);
    j++;
    Control_2(j * 30);
    if (j > 6)
    {
      j = 6;
    }
  }
}

void bopan_2()
{
  temp++;
  if (temp == 1)
  {
    j--;
    Control_2(j * 30);
    delay_us(100000);
    j--;
    Control_2(j * 30);
    if (j < 0)
    {
      j = 0;
    }
  }
}
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void key_scan(void)
{
  switch (PS2_KEY)
  {
  case 0:
//    temp = 0;
//    stop();
    break;

  case 1:
    break;

  case 3:
    break;

  case 4:
    break;

  case 5:
    // go(L2MOTRO_MAX, R2MOTRO_MAX, L1MOTRO_MAX, R1MOTRO_MAX);
    break;

  case 6:
    // right(L2MOTRO_MAX, R2MOTRO_MAX, L1MOTRO_MAX, R1MOTRO_MAX);
    break;

  case 7:
    // back(L2MOTRO_MAX, R2MOTRO_MAX, L1MOTRO_MAX, R1MOTRO_MAX);
    break;

  case 8:
    // left(L2MOTRO_MAX, R2MOTRO_MAX, L1MOTRO_MAX, R1MOTRO_MAX);
    break;

  case 9:
//    servo2_ang-=SERVO2_STEP;
//		servo2_ang=servo2_ang<SERVO2_MIN?0:servo2_ang;
	
	servo2_ang=SERVO2_MIN;
    break;

  case 10:
//		servo1_ang+=SERVO1_STEP;
//		servo1_ang=servo1_ang>SERVO1_MAX?SERVO1_MAX:servo1_ang;
	servo1_ang=SERVO1_MAX;
    break;

  case 11:
		while(servo2_ang<=SERVO2_MAX)
		{
			
		servo2_ang+=SERVO2_STEP;
		Control_2(servo2_ang);
			delay_us(50000);
			
		}
		servo2_ang=servo2_ang>SERVO2_MAX?SERVO2_MAX:servo2_ang;
	
//	servo2_ang=SERVO2_MAX;
    break;

  case 12:
//		servo1_ang-=SERVO1_STEP;
//		servo1_ang=servo1_ang<SERVO1_MIN?SERVO1_MIN:servo1_ang;
	servo1_ang=SERVO1_MIN;
    break;

  case 13:
    // leftspin(L2MOTRO_MAX, R2MOTRO_MAX, L1MOTRO_MAX, R1MOTRO_MAX);
    break;

  case 14:
    shoot(0, 0);
    break;

  case 15:
    // rightspin(L2MOTRO_MAX, R2MOTRO_MAX, L1MOTRO_MAX, R1MOTRO_MAX);
    break;

  case 16:
    shoot(FAN1MOTRO_MAX, FAN2MOTRO_MAX);
    break;

  default:
    break;
  }
}
void Control_wheel(void)
{
  if (PS2_LX < 0)
    left(fabs(L2MOTRO_MAX * PS2_LX), fabs(R2MOTRO_MAX * PS2_LX), fabs(L1MOTRO_MAX * PS2_LX), fabs(R1MOTRO_MAX * PS2_LX));
  else if(PS2_LX > 0)
    right(fabs(L2MOTRO_MAX * PS2_LX), fabs(R2MOTRO_MAX * PS2_LX), fabs(L1MOTRO_MAX * PS2_LX), fabs(R1MOTRO_MAX * PS2_LX));

  if (PS2_LY < 0)
    go(fabs(L2MOTRO_MAX * PS2_LY), fabs(R2MOTRO_MAX * PS2_LY), fabs(L1MOTRO_MAX * PS2_LY), fabs(R1MOTRO_MAX * PS2_LY));
  else if(PS2_LY > 0)
    back(fabs(L2MOTRO_MAX * PS2_LY), fabs(R2MOTRO_MAX * PS2_LY), fabs(L1MOTRO_MAX * PS2_LY), fabs(R1MOTRO_MAX * PS2_LY));

  if (PS2_RX < 0)
    leftspin(fabs(L2MOTRO_MAX * PS2_RX * SPIN_RATIO), fabs(R2MOTRO_MAX * PS2_RX* SPIN_RATIO), fabs(L1MOTRO_MAX * PS2_RX* SPIN_RATIO), fabs(R1MOTRO_MAX * PS2_RX* SPIN_RATIO));
  else if(PS2_RX > 0)
    rightspin(fabs(L2MOTRO_MAX * PS2_RX* SPIN_RATIO), fabs(R2MOTRO_MAX * PS2_RX* SPIN_RATIO), fabs(L1MOTRO_MAX * PS2_RX* SPIN_RATIO), fabs(R1MOTRO_MAX * PS2_RX* SPIN_RATIO));
	
	if(PS2_LX==0&&PS2_LY==0&&PS2_RX==0)
		stop();
}
/* USER CODE END 0 */

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  MX_TIM4_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim2); // 启动Tim2
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4); // 启动pwm
  HAL_TIM_Base_Start_IT(&htim3);            // 启动Tim2
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);

  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); // 启动pwm
  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2); // 启动pwm
  PS2_SetInit();
  Control_1(servo1_ang);
  Control_2(servo2_ang);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		Control_1(servo1_ang);
		Control_2(servo2_ang);
    //		HAL_GPIO_WritePin(board_led_GPIO_Port,board_led_Pin,GPIO_PIN_SET);
    delay_us(50000);
    //		for(i=0;i<30000;i++);
    PS2_Receive();
    key_scan();
    Control_wheel();
    // shoot(6000,6000);
    //	HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);
    // HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
    //	__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_1,5000);
    //		HAL_GPIO_WritePin(board_led_GPIO_Port,board_led_Pin,GPIO_PIN_RESET);
    // Control_2(tst_ang);
  }
  /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
   * in the RCC_OscInitTypeDef structure.
   */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
   */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
